package org.mindrive.system.web.expenses;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.mindrive.system.annotation.LogOperation;
import org.mindrive.system.collection.expenses.ApprovalEntity;
import org.mindrive.system.collection.expenses.DetailEntity;
import org.mindrive.system.core.Result;
import org.mindrive.system.parameter.expenses.*;
import org.mindrive.system.service.BaseService;
import org.mindrive.system.service.expenses.ApprovalService;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.web.BaseController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * (Approval)表控制层
 *
 * @author maweihong
 * @since 2023-10-30 14:22:27
 */

@Api(tags = {"报表管理"})
@RestController
@RequestMapping("/approval")
@Slf4j
public class ApprovalController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private ApprovalService approvalService;

    /**
     * 分页查询所有数据
     *
     * @param parameter 查询实体
     * @return 所有数据
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询审核单列表(可分页)")
    @ApiOperation(value = "查询审核单列表(可分页)", notes = "查询审核单列表(可分页)")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/search")
    public Result search(@RequestBody ApprovalSearchParameter parameter) {
        log.info("查询信息列表开始。");
        Result result = new Result();
        try {
            Map<String, Object> data = approvalService.search(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {
            log.error("分页查询所有数据失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("分页查询所有数据结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "取得详细")
    @ApiOperation(value = "取得详细", notes = "取得详细")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping(path = "/detail/{id}")
    public Result getDetail(@PathVariable(value = "id") String id) {
        log.info("取得详细开始。");
        Result result = new Result();
        try {
            ApprovalEntity data = approvalService.detail(id);
            result = Result.ok(data);
        } catch (Exception ex) {
            log.error("取得详细失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("取得详细结束。");
        return result;
    }


    /**
     * 新增数据
     *
     * @param parameter 实体对象
     * @return 新增结果
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "报表新增")
    @ApiOperation(value = "报表新增", notes = "返回单个对象")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/insert")
    public Result insert(@Validated @RequestBody ApprovalParameter parameter, BindingResult bindingResult) {
        Result result = new Result();
        try {
            validParams(bindingResult);
            ApprovalEntity data = approvalService.create(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {
            log.error("新增失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("新增数据结束。");
        return result;
    }

    /**
     * 修改数据
     *
     * @param parameter 实体对象
     * @return 修改结果
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "报表修改")
    @ApiOperation(value = "报表修改", notes = "返回修改后结果")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id, @Validated @RequestBody ApprovalParameter parameter,
                         BindingResult bindingResult) {
        Result result = new Result();
        try {
            approvalService.modify(id, parameter);
            result = Result.ok();
        } catch (Exception ex) {
            log.error("修改失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("修改数据结束。");
        return result;
    }

    /**
     * 删除数据(物理删除)
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_DELETE, event = "删除数据")
    @ApiOperation(value = "删除数据(逻辑删除)", notes = "返回删除结果")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/delete")
    public Result delete(@RequestBody List<String> ids) {
        log.info("删除数据开始。");
        Result result = new Result();
        try {
            approvalService.delete(ids);
            result = Result.ok();
        } catch (Exception ex) {
            log.error("删除数据失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("删除数据结束。");
        return result;
    }

    /**
     * @param parameter
     * @describe 报表信息填报
     * @author lsy
     * @DATE 2023/10/30 15:22
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "报表详细信息填报")
    @ApiOperation(value = "报表详细信息填报", notes = "报表详细信息填报")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/detail/insert")
    public Result detailInsert(@Validated @RequestBody DetailParameter parameter, BindingResult bindingResult) {
        Result result = new Result();
        try {
            validParams(bindingResult);
            DetailEntity data = approvalService.detailInsert(parameter);
            result = Result.ok(data);
        } catch (Exception ex) {
            log.error("新增失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("新增数据结束。");
        return result;
    }

    /**
     * @param parameter
     * @describe 报表信息填报(格式修改)
     * @author lsy
     * @DATE 2023/10/30 15:22
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_CREATE, event = "报表详细信息填报(格式修改)")
    @ApiOperation(value = "报表详细信息填报(格式修改)", notes = "报表详细信息填报(格式修改)")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping(path = "/detail/insertgai")
    public Result detailInsertgai(@Validated @RequestBody List<List<Map<String , Object>>> parameter, BindingResult bindingResult) {
        Result result = new Result();
        try {
            validParams(bindingResult);
            approvalService.detailInsertgai(parameter);
            result = Result.ok();
        } catch (Exception ex) {
            log.error("新增失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("新增数据结束。");
        return result;
    }


    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询审核单内容列表")
    @ApiOperation(value = "查询审核单内容列表", notes = "查询审核单内容列表")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping(path = "/searchDetailList/{id}")
    public Result searchDetailList(@PathVariable(value = "id") String id){
        log.info("查询信息列表开始。");
        Result result = new Result();
        try {
            final List<DetailEntity> list = approvalService.searchDetailList(id);
            result = Result.ok(list);
        } catch (Exception ex) {
            log.error("分页查询所有数据失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("分页查询所有数据结束。");
        return result;
    }

    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_SEARCH, event = "查询审核单内容列表(格式修改)")
    @ApiOperation(value = "查询审核单内容列表(格式修改)", notes = "查询审核单内容列表(格式修改)")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping(path = "/searchDetailListGai/{id}")
    public Result searchDetailListGai(@PathVariable(value = "id") String id){
        log.info("查询信息列表开始。");
        Result result = new Result();
        try {
            final List<List<Map<String, Object>>> lists = approvalService.searchDetailListGai(id);
            result = Result.ok(lists);
        } catch (Exception ex) {
            log.error("分页查询所有数据失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("分页查询所有数据结束。");
        return result;
    }

    /**
     * @describe 科室主任审核
     * @param parameter
     * @author lsy
     * @DATE 2023/10/30 17:22
     */
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "科室主任审核")
    @ApiOperation(value = "科室主任审核", notes = "科室主任审核")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PutMapping("directorUpdate/{id}")
    public Result directorUpdate(@PathVariable(value = "id") String id, @Validated @RequestBody DirectorUpdateApprovalParameter parameter,
                         BindingResult bindingResult) {
        Result result = new Result();
        try {
            approvalService.directorUpdate(id, parameter);
            result = Result.ok();
        } catch (Exception ex) {
            log.error("修改失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("修改数据结束。");
        return result;
    }

    //财务审核
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_MODIFY, event = "财务审核")
    @ApiOperation(value = "财务审核", notes = "财务审核")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PutMapping("financeUpdate/{id}")
    public Result financeUpdate(@PathVariable(value = "id") String id, @Validated @RequestBody FinanceUpdateApprovalParameter parameter,
                                 BindingResult bindingResult) {
        Result result = new Result();
        try {
            approvalService.financeUpdate(id, parameter);
            result = Result.ok();
        } catch (Exception ex) {
            log.error("修改失败。", ex);
            result = Result.error(ex.getMessage());
        }
        log.info("修改数据结束。");
        return result;
    }

    //报表导出
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_EXPORT, event = "报表导出")
    @ApiOperation(value = "报表导出", notes = "报表导出")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @GetMapping("/expertDerive/{approvalId}")
    public void expertDerive(@PathVariable(value = "approvalId")String approvalId, HttpServletResponse response) throws Exception{
        log.info("报表导出开始。");
        try {
            approvalService.expertDerive(approvalId , response);
        }catch (Exception ex) {
            log.error("报表导出失败。", ex);
        }
        log.info("报表导出结束。");
    }

    //银行报表导出
    @LogOperation(type = BaseService.AUDIT_EVENT_TYPE_EXPORT, event = "银行报表导出")
    @ApiOperation(value = "银行报表导出", notes = "银行报表导出")
    @ApiImplicitParam(name = "authorization", value = "令牌", required = true, paramType = "header")
    @PostMapping("/expertBankDerive")
    public void expertBankDerive(@Validated @RequestBody ExpertParameter parameter, HttpServletResponse response) throws Exception{
        log.info("报表导出开始。");
        try {
            approvalService.expertBankDerive(parameter , response);
        }catch (Exception ex) {
            log.error("报表导出失败。", ex);
        }
        log.info("报表导出结束。");
    }
}

